﻿using MediatR;
using Newtonsoft.Json;
using System.Collections.Generic;
using Warehouse.Domain;
using Warehouse.Domain.DTO;
using Warehouse.Infrstructre.Interface;
using Warehouse.Read.API.Applcation.Command.EnterStoragesCommand;
using static WMSpublic.ParseAPI;
using WMSpublic;
using System.ComponentModel.DataAnnotations;

namespace Warehouse.Read.API.Applcation.CommandHandlers.EnterStoragesCommandHandler
{
    public class EnterStoragesSetCommandHandlers : IRequestHandler<EnterStoragesSetCommand, EnterStoragesSetDto>
    {
        protected readonly IEnterStoragesInfoRepsitory infoRep;
        protected readonly IEnterStoragesRepsitory entRep;
        public EnterStoragesSetCommandHandlers(IEnterStoragesInfoRepsitory infoRep, IEnterStoragesRepsitory entRep)
        {
            this.infoRep = infoRep;
            this.entRep = entRep;
        }

        public async Task<EnterStoragesSetDto> Handle(EnterStoragesSetCommand request, CancellationToken cancellationToken)
        {
            EnterStoragesSetDto dto = new EnterStoragesSetDto();
            List<Products> products = await GetProductsInfo(1, 100);
            List<StoreHouse> kuwei = await GetStoreInfo(1, 100);
            var arr = await entRep.GetModelAsync(request.StockId);
            var list = from a in await infoRep.GetAllAsyns()
                       join
                       b in products on a.ProductsID equals b.ProductsID
                       join c in kuwei on a.Binlocation equals c.StorehouseId
                       where a.StockId == request.StockId
                       select new newStockProductss
                       {
                           StorehouseId = c.StorehouseId,
                           StorehouseName = c.StorehouseName,
                           Specs = b.Specs,
                           ProductsCode = b.ProductsCode,
                           ProductsName = b.ProductsName,
                           StockProductId = a.StockProductId,
                           StockId = a.StockId,
                           ProductsID = b.ProductsID,
                           Batch = a.Batch,
                           Sprice = a.Sprice,
                           Quantity = a.Quantity,
                           Binlocation = a.Binlocation,
                           StateSP = a.StateSP,
                           ShenHePeople = a.ShenHePeople,
                           ShenHeTime = a.ShenHeTime,
                           office_id = a.office_id,
                           user_id = a.user_id,
                           AddName = a.AddName,
                           AddTime = a.AddTime,
                           UpdName = a.UpdName,
                           UpdTime = a.UpdTime,
                           DelName = a.DelName,
                           DelTime = a.DelTime,
                           IsDel = a.IsDel,

                       };

            dto.StockId = arr.StockId;
            dto.StockCode = arr.StockCode;
            dto.StockOrderType = arr.StockOrderType;
            dto.SorderNumber = arr.SorderNumber;
            dto.VendorId = arr.VendorId;
            dto.VendorName = arr.VendorName;
            dto.VendorContact = arr.VendorContact;
            dto.VendorPhone = arr.VendorPhone;
            dto.MakePerson = arr.MakePerson;
            dto.MarkTime = arr.MarkTime;
            dto.office_id = arr.office_id;
            dto.AddName = arr.AddName;
            dto.AddTime = arr.AddTime;
            dto.UpdName = arr.UpdName;
            dto.UpdTime = arr.UpdTime;
            dto.DelName = arr.DelName;
            dto.DelTime = arr.DelTime;
            dto.IsDel = arr.IsDel;
            dto.SRremark = arr.SRremark;
            dto.stockProducts = list.ToList();
            return dto;
        }


        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetProductsInfo(int pageindex, int pagesize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyNCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMSIsImp0aSI6IjQzMzA2NmRmLWYyNjAtNDI2Ny1iYWY5LWJkMzgxZGVhZGMwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiLmnLHms4rpnJYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy91cmkiOiIiLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.E0PG5CbU-_2efHW14aTC5dmRc3FC7BLU0TIgjNsA1wo";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/cpd/api/ProductRead/GetAllProductInfo?pageindex=" + pageindex + "&pagesize=" + pagesize;
            string postRep = await client.GetAsync(url);
            var vendors = JsonConvert.DeserializeObject<ApiResultList<Products>>(postRep);
            return vendors.data.list;
        }
        /// <summary>
        /// 获取库位信息
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetStoreInfo(int pageindex, int pagesize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyNCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMSIsImp0aSI6IjQzMzA2NmRmLWYyNjAtNDI2Ny1iYWY5LWJkMzgxZGVhZGMwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiLmnLHms4rpnJYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy91cmkiOiIiLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.E0PG5CbU-_2efHW14aTC5dmRc3FC7BLU0TIgjNsA1wo";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/ckd/api/ReadStorehouse/GetStorehouseList?pageIndex=" + pageindex + "&PageSize=" + pagesize;
            string postRep = await client.GetAsync(url);
            var vendors = JsonConvert.DeserializeObject<ApiResultList<StoreHouse>>(postRep);
            return vendors.data.list;
        }

        /// <summary>
        /// 产品管理
        /// </summary>
        public class Products : Audit
        {
            /// <summary>
            /// 产品id
            /// </summary>
            [Key]
            public int ProductsID { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            public string? ProductsCode { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            public string? ProductsName { get; set; }
            /// <summary>
            /// 厂商编码
            /// </summary>
            public string? VendorsCode { get; set; }
            /// <summary>
            /// 内部编码
            /// </summary>
            public string? InteriorCode { get; set; }
            /// <summary>
            /// 规格
            /// </summary>
            public string? Specs { get; set; }
            /// <summary>
            /// 产品类别
            /// </summary>
            public int? ProductTypeID { get; set; }
            /// <summary>
            /// 存储单位
            /// </summary>
            public string? StorageUnits { get; set; }
            /// <summary>
            /// 预警（上）
            /// </summary>
            public int? Upperlimit { get; set; }
            /// <summary>
            /// 预警（下）
            /// </summary>
            public int? Lowerlimit { get; set; }
            /// <summary>
            /// 包装类型
            /// </summary>
            public int? WrapType { get; set; }
            /// <summary>
            /// 价格
            /// </summary>
            public decimal? Price { get; set; }
            /// <summary>
            /// 重量
            /// </summary>
            public decimal? Weight { get; set; }
            /// <summary>
            /// 默认仓库
            /// </summary>
            public int? WarehouseId { get; set; }
            /// <summary>
            /// 库位
            /// </summary>
            public int? StorehouseId { get; set; }
            /// <summary>
            /// 默认供应商
            /// </summary>
            public int? VendorId { get; set; }
            /// <summary>
            /// 默认客户
            /// </summary>
            public int? ClientId { get; set; }
            /// <summary>
            /// 描述
            /// </summary>
            public string? Describe { get; set; }
            /// <summary>
            /// 产品数量
            /// </summary>
            public int? ProcurementNum { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? Office_Id { get; set; }

            /// <summary>
            /// 用户id
            /// </summary>
            public int? User_Id { get; set; }
        }
        /// <summary>
        /// 库位表
        /// </summary>
        public class StoreHouse : Audit
        {
            /// <summary>
            /// 主键id
            /// </summary>
            [Key]
            public int StorehouseId { get; set; }
            /// <summary>
            /// 库位编号
            /// </summary>
            public string? StorehouseCode { get; set; }
            /// <summary>
            /// 库位名称
            /// </summary>
            public string? StorehouseName { get; set; }
            /// <summary>
            /// 库位类型
            /// </summary>
            public string? StorehouseType { get; set; }
            /// <summary>
            /// 仓库id
            /// </summary>
            public int? WarehouseId { get; set; }
            /// <summary>
            /// 是否禁用
            /// </summary>
            public bool? IsDisable { get; set; }
            /// <summary>
            /// 是否默认
            /// </summary>
            public bool? IsDefaults { get; set; }
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime? CreatTime { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string? Sremark { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? office_id { get; set; }
            /// <summary>
            /// 用户id
            /// </summary>
            public int? user_id { get; set; }

        }
    }
}
